/* 刷新页面时重置表单 */
window.onload = function () {
    $("form").each(function (index, element) {
        element.reset();
    });
}

$(document).ready(function () {
    //判断是不是推荐链接进来的
    var recommendId = $.cookie("recommendId");
    if ($.trim(recommendId) != "") {
        $("#recommend-user-dl").hide();
    } else {
        $("#recommend-user-dl").show();
    }
    $(".btnSendCode").click(function () {
        checkPhone(function () {
            var phone = $("#phone").val();

            var param = {
                captchaType: 1,
                captchaWay: phone,
                captchaUse: 601
            };
            AQ.callAQ("applyCaptcha.do", param, function (result) {
                var dom = $(".btnSendCode");
                countDown(dom[0], 60);
                $("#phone").prop('readOnly', true);
                setTimeout(function () {
                    $("#phone").prop('readOnly', false);
                }, 60 * 1000);
            }, function (err, code) {
                Utils.msgDialog(err + "[" + code + "]");
            });
        });
    });

    /**
     * 点击注册按钮
     */
    $(".reg_sub").click(function () {
        regist();
    });

});


/* 点击注册按钮 */
function regist() {
    if (!(checkUName() && checkPwd1() && checkPwd2() && checkQQ() && checkPhone() && checkCode())) {
        return;
    }

    var userName = $.trim($("#userName").val());
    var passWord = $.trim($("#r_pwd").val());
    var qq = $.trim($("#qq").val());
    var phone = $.trim($("#phone").val());
    var captchaCode = $.trim($("#ph_code").val());
    var recommendId = $.cookie("recommendId");
    var recommendUserQQ = $.trim($("#recommend-user").val());

    $(".reg_sub").prop("disabled", true);
    $(".reg_sub").val("正在注册···");

    var param = {
        userName: userName,
        passWord: passWord,
        phone: phone,
        captchaCode: captchaCode,
        qq: qq,
        recommendId: recommendId,
        recommendUserQQ: recommendUserQQ,
    };
    AQ.callAQ("createUser.do", param, function (result) {
        $.cookie("userName", result[1].userName, {expires: 30});
        $.cookie("User", JSON.stringify(result[1]));
        if (result[1].experience < 30) {
            $.cookie("firstLogin", true);
        }
        location.replace("reg-success.html");
    }, function (err, code) {
        if (code == 11) {
            Utils.checkDialog(err, "不填写推荐人", "返回更正", function () {
                $("#recommend-user").val("");
                regist();
            }, function () {
            });
        } else {
            Utils.msgDialog(err + "[" + code + "]");
        }
        $(".reg_sub").val("注  册");
        $(".reg_sub").prop("disabled", false);
    });
};

/* 检测用户名 */
function checkUName() {
    var userName = $("#userName").val();
    var msg = "";
    var retn = false;
    var nameReg = /^[\u4E00-\u9FA50-9A-Za-z_-]{3,25}$/;
    var numReg = /^[0-9]+$/;
    if (userName == "") {
        msg = "请输入用户名";
    } else if (!nameReg.test(userName)) {
        msg = "用户名格式不正确";
    } else if (numReg.test(userName)) {
        msg = "用户名不能全为数字";
    } else if (Utils.countLength(userName) < 5 || Utils.countLength(userName) > 25) {
        msg = "用户名长度不合法";
    } else {
        retn = true;
        var param = {
            userKey: userName,
            keyType: 1,
        };
        AQ.callAQ("isUserKeyUsed.do", param, function (result) {
            retn = true;
        }, function (err, code) {
            if (code == 2) {
                msg = "用户名已存在";
            } else {
                msg = err;
            }
            retn = false;
        });
    }
    if (!retn) {
        toastr.error(msg);
    }
    return retn;
}

/* 检测密码1 */
function checkPwd1() {
    var pwd = $.trim($("#pwd").val());
    if (pwd == "") {
        toastr.error("请输入密码");
        return false;
    }
    var pwdReg = /^[0-9A-Za-z!@#$%&*_-]{6,25}$/;
    if (!pwdReg.test(pwd)) {
        toastr.error("密码格式不正确");
        return false;
    } else {
        return true;
    }
}

/* 检测密码2 */
function checkPwd2() {
    var pwd = $.trim($("#pwd").val());
    var r_pwd = $.trim($("#r_pwd").val());
    if (r_pwd == "") {
        toastr.error("请输入确认密码");
        return false;
    }
    var pwdReg = /^[0-9A-Za-z!@#$%&*_-]{6,25}$/;
    if (!pwdReg.test(r_pwd)) {
        toastr.error("密码格式不正确");
        return false;
    } else if (!(pwd == r_pwd)) {
        toastr.error("两次密码不一致");
        return false;
    } else {
        return true;
    }
}

/* 检测QQ */
function checkQQ() {
    var qq = $.trim($("#qq").val());
    if (qq == "") {
        toastr.error("请输入QQ号");
        return false;
    }
    var qqReg = /^[0-9]{5,11}$/;
    if (!qqReg.test(qq)) {
        toastr.error("QQ格式不正确");
        return false;
    } else {
        return true;
    }
}

/* 
 检测手机号
 @param succFunc {function} 手机号验证通过回调函数
 */
function checkPhone(succFunc) {
    var phone = $.trim($("#phone").val());
    if (phone == "") {
        toastr.error("请输入手机号");
        return false;
    }
    var phoneReg = /^[1][34578][0-9]{9}$/;
    if (!phoneReg.test(phone)) {
        toastr.error("手机格式不正确");
        return false;
    }
    var retn = true;

    var param = {
        userKey: phone,
        keyType: 2,
    };
    AQ.callAQ("isUserKeyUsed.do", param, function (result) {
        succFunc();
        retn = true;
    }, function (err, code) {
        if (code == 2) {
            toastr.error("手机号已被注册");
        } else {
            toastr.error(err);
        }
        retn = false;
    });
    return retn;
}

/* 检测验证码 */
function checkCode() {
    var ph_code = $.trim($("#ph_code").val());
    if (ph_code == "") {
        toastr.error("请输入验证码");
        return false;
    }
    var codeReg = /^[0-9]{6}$/;
    if (!codeReg.test(ph_code)) {
        toastr.error("验证码格式不正确");
        return false;
    } else {
        return true;
    }
}
function login(){
    location.replace("login.html");
}